//
//  MDDActionViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/9.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDActionViewController.h"
#import "ActionItemModel.h"
#import "ActionItemCell.h"
#import "ActionDetailViewController.h"
#import "CBSegmentView.h"
#import "AppKindsModel.h"

@interface MDDActionViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,strong) NSString *key;
@property (nonatomic,strong) NSMutableArray *kindsArr;
@property (nonatomic, strong) CBSegmentView *zoomSegmentView;

@property (weak, nonatomic) IBOutlet UIButton *fabuBtn;

@end

@implementation MDDActionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"车友圈");
    self.key = nil;
    self.page = 1;
    self.dataArr = [[NSMutableArray alloc]init];
    
    self.fabuBtn.axcUI_buttonContentLayoutType = 2;
    self.fabuBtn.axcUI_padding = 2;
    [self.fabuBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [self getBbsType];
    [self loadAllHuDongs];
    self.selfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadAllHuDongs];
    }];
    self.selfTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadAllHuDongs];
    }];
    WS(weakSelf);
    [self.view addSubview:self.zoomSegmentView];
    self.zoomSegmentView.titleChooseReturn = ^(NSInteger x) {
        NSLog(@"点击了第%ld个按钮",x+1);
        if (x == 0) {
            weakSelf.key = nil;
        }else{
            AppKindsModel * kindsModel = weakSelf.kindsArr[x];
            weakSelf.key = kindsModel.ID;
        }
        [weakSelf.selfTableView.mj_header beginRefreshing];
    };
}

#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActionItemCell * itemCell = mGetTableViewCell(@"actionItemCell");
    ActionItemModel * itemModel = self.dataArr[indexPath.row];
    [itemCell reloadViewForModel:itemModel];
    return itemCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ActionItemCell cellForHeightFor:self.dataArr[indexPath.row]];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActionDetailViewController * advc = mGetStoryboardViewController(@"Main", @"actionDetailViewController");
    ActionItemModel * itemModel = self.dataArr[indexPath.row];
    advc.bbsId = itemModel.ID;
    if ([itemModel.uid isEqualToString:[AppUserBaseModel sharedUserData].uid]) {
        advc.isSelf = YES;
    }
    NavigationController_pushVC(advc);
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadAllHuDongs
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    if (self.key) {
        setDickeyobj(parameters, self.key, @"key");
    }
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    [PPNetworkHelper POST:app_circle_getReleaseList parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                ActionItemModel * item = [[ActionItemModel alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                item.ID = dataD[@"id"];
                [self.dataArr addObject:item];
            }
            
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];

            }
           
        }else{
            
        }
         [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
-(void)getBbsType
{
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    [PPNetworkHelper POST:app_circle_getBbsType parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDci in responseObject[@"data"]) {
                AppKindsModel * model = [[AppKindsModel alloc]init];
                [model setValuesForKeysWithDictionary:infoDci];
                model.ID = infoDci[@"id"];
                [self.kindsArr addObject:model];
                [self.zoomSegmentView setTitleArray:self.kindsArr withStyle:CBSegmentStyleSlider];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
    
    } showLoading:nil];
}
#pragma mark -
#pragma mark - **************** lazyLoad
-(CBSegmentView *)zoomSegmentView{
    if (!_zoomSegmentView) {
        _zoomSegmentView = [[CBSegmentView alloc]initWithFrame:CGRectMake(0, getRectNavAndStatusHight, mScreenWidth, 44)];
        _zoomSegmentView.backgroundColor = [UIColor whiteColor];
    }
    return _zoomSegmentView;
}
-(NSMutableArray *)kindsArr
{
    if (!_kindsArr) {
        _kindsArr = [[NSMutableArray alloc]init];
        [_kindsArr addObject:@"全部"];
    }
    return _kindsArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
